program mainhop
	use MIMIMI
	
	implicit NONE
	
	integer(4), parameter			:: NINDXESMAX = 2*LMAX+LX*LX
	integer(4), parameter			:: nmax = 300000
	integer(4), parameter			:: nstart = 50000
	
	integer(4)					:: LC
	
	integer(4)					:: nmc
	integer(4)					:: tt
	real(8)						:: beta, t
	integer(4)					:: H(3, LMAX)
	
	integer(4)					:: ix(NINDXESMAX)
	integer(4)					:: nindxes
	integer(4)					:: kx
	integer(4)					:: inac
	integer(4)					:: nonzero
	
	real(8)						:: nn(LX,LX), nstep(LX,LX)
!=======================================================
	tt = 2
		
	LC = 5
	!nindxes = 3*LC
	nindxes = 2*LC + LX*LX
	
	t = tt * 0.5d0
	beta = 1.0d0 / t
		
	call Init(LC) 
		
	H(:,1:LC) = 0
	sites = 0
	sites( LX/2 + 1, LX/2 + 1) = 1
	
	do inac = 1, nstart
		call SetRandomSequence(nindxes,ix(1:nindxes))
		do kx = 1, nindxes
			if ( ix(kx) <= LC) then
				call LocalDuostep(beta,LC,H(:,1:LC))
			else if (( ix(kx) <= 2*LC).and.( ix(kx) <= LC)) then
				call LocalUniStep(beta,LC,H(:,1:LC))
			else
				call Turn(LC, H(:,1:LC))
			end if
		end do
			
		!call SetRandomSequence(nindxes,ix(1:nindxes))
		!do kx = 1, nindxes
		!	if ( ix(kx) <= LX*LX) then
		!		call GlobalStep(LC,H(:,1:LC))
		!	else if ( ix(kx) <= LX*LX + LC) then
		!		call LocalDuostep(beta,LC,H(:,1:LC))
		!	else
		!		call LocalUniStep(beta,LC,H(:,1:LC))
		!	end if
		!end do
		
		nonzero = count( H(1,1:LC) /= 0)
		if (nonzero >= 6) then
			continue
		end if
		
		if ( nonzero > 0.95d0 * LC) then
			if (LC+5 > LMAX) stop "AZAZA"
			H(:,LC+1:LC+5) = 0
			LC = LC + 5
			call Init(LC)
			nindxes = 2*LC+LX*LX
			print *, inac, LC
		end if
			
	end do
	print *, LC
	nn = 0.0d0
	nstep = 0.0d0

	do inac = 1, nmax
		call SetRandomSequence(nindxes,ix(1:nindxes))
		do kx = 1, nindxes
			
			continue
				
			if ( ix(kx) <= LC) then
				call LocalDuostep(beta,LC,H(:,1:LC))
			else
				call LocalUniStep(beta,LC,H(:,1:LC))
			end if
				
		end do
		
		call CalcN( nstep, LC, H)
		nn = nn + nstep
			
	end do
	
	nn = nn / nmax
	
	do kx = 1, LX
		print '(10F10.3)', nn(kx,:)
	end do
	
end program mainhop